<template>
	<view class="content pageBg">
		<nav-bar :header="userInfo.avatarUrl" :score="score" :level="level" @addScoreClick="addScoreClick"></nav-bar>
		<view class="main">
			<view class="item" v-for="(item,index)  in dataList" :key="item" @click="itemClick(item,index)">
				<view class="bg">
					<image :src="item.bg" mode="aspectFill"></image>
				</view>
				<view class="text">
					{{item.name}}
				</view>
				<view class="score" @click="$emit('addScoreClick')">
					<image class="coin" src="../../static/logo.png" mode="aspectFill"></image>
					<view class="money">
						50
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue"
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import {
		getUserOpenId
	} from "../../utils/getOpenId";
	import {
		onShareAppMessage,
		onShareTimeline
	} from "@dcloudio/uni-app";
	import {
		getUserScore,
		SignInGetScore,
		addUserScore
	} from "../../utils/common";
	import {
		getUserInfo
	} from "../../store/user";
	const score = ref(0);
	const level = ref(1);
	const usertype = ref(1);
	const dataList = ref([])
	const userInfo = ref({})
	const db = uniCloud.database();
	onLoad(() => {
		score.value = 1000;
		level.value = 2;
		
		mutations.updateUserInfo({score:2000});
	})

	onShow(() => {
		score.value = getUserScore()
		db.collection("guaguale-list").orderBy('index desc').get().then(res => {
			if (res.result.data && res.result.data.length > 0) {
				dataList.value = res.result.data;
			}
		});
		getUserInfoMethods();

	})

	function addScoreClick() {
		score.value = SignInGetScore()
	}

	function getUserInfoMethods() {
		uni.login({
			timeout: 6000,
			success: (res) => {
				console.log('success:login方法返回的值：', res)
				uni.getUserInfo({
					success(userInfo) {
						console.log('用户信息：', userInfo)
					}
				})
			},
			fail(err) {
				console.log('fail:login方法返回错误：', err)
			}
		})
	}

	function getOpenID() {
		getUserOpenId().then(res => {
			console.log("openID", res);

		})
		getUserInfo(e => {
			console.log("openID:", e);
		})
	}

	function itemClick(item, index) {
		const score = addUserScore(-50);
		if (score >= 0) {
			console.log(item + index);
			const urlStr = '/pages/detail/detail' + (item.type) + '?title=' + item.name+'&usertype='+usertype.value;
			console.log('url:' + urlStr);
			uni.navigateTo({
				url: urlStr
			})
		}
	}

	//分享给好友
	onShareAppMessage((e) => {
		return {
			title: "喜乐刮刮卡，你离百万富翁就差刮一刮",
			path: "/pages/index/shouye"
		}
	})

	//分享朋友圈
	onShareTimeline(() => {
		return {
			title: "喜乐刮刮卡，你离百万富翁就差刮一刮",
			path: "/pages/index/shouye"
		}
	})
</script>

<style lang="scss">
	@font-face {
		font-family: NumFont;
		src: url('https://mp-02da6b36-ab1b-494d-b346-d6763fb77772.cdn.bspapp.com/resource/font/HONORSans-Bold.ttf');
	}

	.main {
		position: relative;
		width: 100%;
		display: grid;
		grid-template-columns: repeat(1, 1fr);
		row-gap: 10rpx;
		column-gap: 20rpx;
		padding: 75rpx;

		.item {
			width: 600rpx;
			height: 900rpx;
			margin-top: 20rpx;
			position: relative;

			.bg {
				left: 0rpx;
				top: 0rpx;

				image {
					border-radius: 20rpx;
					width: 100%;
					height: 100%;
				}
			}

			.text {
				position: absolute;
				text-align: center;
				z-index: inherit;
				width: 100%;
				top: 400rpx;
				left: 0;
				color: white;
				font-family: 'NumFont';
				font-size: 120rpx;
				background-color: rgba(0, 0, 0, 0.5);
				font-weight: bold;
			}

			.score {
				position: absolute;
				top: 40rpx;
				right: 50rpx;
				height: 60rpx;
				width: 150rpx;
				align-items: center;
				display: flex;
				flex-direction: row;
				font-size: 22px;
				font-weight: 700;
				margin-left: 30rpx;
				padding: 5rpx 10rpx;
				border-radius: 45rpx;
				background-color: rgba(0, 0, 0, 0.5);

				.coin {
					height: 50rpx;
					width: 50rpx;
				}

				.money {
					color: white;
					margin-left: 20rpx;
					margin-right: 20rpx;
				}
			}
		}
	}

	.header {
		position: relative;
		top: 0;
		left: 0;
		width: 100%;
		height: 166rpx;
		background-color: transparent;
		/* background-color: black; */
		display: flex;
		flex-direction: row;
		align-items: bottom;

		.level {
			bottom: 10rpx;
			height: 60rpx;
			color: black;
		}

	}

	.content {
		position: absolute;
		width: 100%;
		height: 100%;

		/* background-image: url("/static/bg.png");
		background-size: cover; */
	}

	.bg {
		position: absolute;
		width: 100%;
		height: 100%;
	}
</style>